package com.example.himalaya.view.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.example.himalaya.view.adapters.TrackListAdapter;
import com.example.himalaya.base.BaseApplication;
import com.example.himalaya.view.callback.ILoveSongViewCallback;
import com.example.himalaya.presenters.LoveSongPresenter;
import com.example.himalaya.presenters.PlayerPresenter;
import com.example.himalaya.view_component.ConfirmCheckBoxDialog;
import com.example.himalaya.view_component.UILoader;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;
import java.util.Objects;

public class LoveSongViewActivity extends AppCompatActivity implements ILoveSongViewCallback, TrackListAdapter.ItemClickListener, TrackListAdapter.ItemLongClickListener, ConfirmCheckBoxDialog.onDialogActionClickListener {


    // Constant
    private final String TAG = "LoveSongViewActivity";

    // UI object
    private RecyclerView mLoveSongList;
    private FrameLayout mLoveContainer;

    // Other UI Object
    private UILoader mUiLoader = null;

    // Logic Object
    private LoveSongPresenter mLoveSongPresenter;


    private TrackListAdapter mTrackListAdapter;
    private Context mContext;
    private Track mCurrentClickLoveSongItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_love_song);


        initVariable();

        initPresenter();

        initView();

        initData();
    }

    private void initPresenter() {
        mLoveSongPresenter = LoveSongPresenter.getInstance();

        mLoveSongPresenter.registerViewCallback(this);
    }

    private void initData() {
        mLoveSongPresenter.listLoveSongs();
    }

    private void initView() {
        if (mUiLoader == null) {

            mUiLoader = new UILoader(BaseApplication.getAppContext()) {
                @Override
                protected View getSuccessView(ViewGroup container) {
                    return createSuccessView(container);
                }
            };


            mLoveContainer.removeAllViews();
            mLoveContainer.addView(mUiLoader);
        }


        mUiLoader.updateStatus(UILoader.UIStatus.LOADING);
    }

    private void initVariable() {

        mContext = BaseApplication.getAppContext();

        mLoveContainer = this.findViewById(R.id.love_song_container);
    }

    private View createSuccessView(ViewGroup container) {
        View mSuccessView = initSuccessVariable(container);


        initSuccessPresenter();

        initSuccessAdapter();

        initSuccessListener();

        initSuccessView();

        return mSuccessView;
    }

    private void initSuccessAdapter() {
        mTrackListAdapter = new TrackListAdapter();

        mLoveSongList.setAdapter(mTrackListAdapter);
    }

    private View initSuccessVariable(ViewGroup container) {
        View view = LayoutInflater.from(BaseApplication.getAppContext()).inflate(R.layout.song_list, container, false);

        mLoveSongList = view.findViewById(R.id.song_list);

        // TwinklingRefreshLayout refreshLayout = view.findViewById(R.id.love_song_refresh_layout);

        return view;
    }

    private void initSuccessPresenter() {

    }

    private void initSuccessListener() {
        mTrackListAdapter.setItemClickListener(this);
        mTrackListAdapter.setItemLongClickListener(this);
    }

    private void initSuccessView() {
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(BaseApplication.getAppContext());
        mLoveSongList.setLayoutManager(layoutManager);

        mLoveSongList.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.top = UIUtil.dip2px(view.getContext(), 5);
                outRect.bottom = UIUtil.dip2px(view.getContext(), 5);
                outRect.left = UIUtil.dip2px(view.getContext(), 5);
                outRect.right = UIUtil.dip2px(view.getContext(), 5);
            }
        });
    }

    @Override
    public void onLoveSongAdd(boolean isSuccess) {
        mLoveSongPresenter.listLoveSongs();
    }

    @Override
    public void onLoveSongDel(boolean isSuccess) {
        mLoveSongPresenter.listLoveSongs();
    }

    @Override
    public void onLoveSongLoaded(List<Track> tracks) {

        if (mUiLoader == null) {
            return;
        }

        if (tracks != null) {
            if (tracks.size() == 0) {
                mUiLoader.updateStatus(UILoader.UIStatus.EMPTY);
            } else {
                mUiLoader.updateStatus(UILoader.UIStatus.SUCCESS);

                mTrackListAdapter.setData(tracks);
            }
        } else {
            mUiLoader.updateStatus(UILoader.UIStatus.ERROR);
        }
    }

    @Override
    public void onLoveSongClean(boolean isSuccess) {

    }

    @Override
    public void onLoveSongFull() {

    }

    @Override
    public void onItemClick(List<Track> detailData, int position) {
        // 设置播放器的数据
        PlayerPresenter playerPresenter = PlayerPresenter.getPlayerPresenter();

        playerPresenter.setPlayList(detailData, position);

        // 跳转到播放器界面
        Intent intent = new Intent(Objects.requireNonNull(BaseApplication.getAppContext()), PlayerViewActivity.class);

        startActivity(intent);
    }

    @Override
    public void onItemLongClick(Track track) {
        // Toast.makeText(getContext(), "长按", Toast.LENGTH_LONG).show();

        mCurrentClickLoveSongItem = track;
        ConfirmCheckBoxDialog dialog = new ConfirmCheckBoxDialog(LoveSongViewActivity.this);


        dialog.setonDialogActionClickListener(this);

        dialog.show();
    }

    @Override
    public void onRightBtnClick() {

    }

    @Override
    public void onLeftBtnClick(boolean checked) {
        if (mLoveSongPresenter != null) {
            if (mCurrentClickLoveSongItem != null) {
                if (!checked) {
                    mLoveSongPresenter.delLoveSong(mCurrentClickLoveSongItem);
                } else {
                    mLoveSongPresenter.clearLoveSong();
                }
            }
        }
    }

}
